/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.content.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.content.ContentDao;
import org.grayrabbit.cms.entity.Category;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.util.DateUtil;
import org.grayrabbit.cms.util.HqlUtil;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Content</b> dao implementation
 * 
 * @version 1.0，创建时间：2011-05-16 14:40:11
 */
@Repository
public class ContentDaoImpl extends GenericHibernateDao<Content, String>implements ContentDao {

	private static Logger LOG = LoggerFactory.getLogger(ContentDaoImpl.class);
	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Content as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * 
	 * @see ContentDao#assembly(ContentBean)
	 */
	public QueryCondition assembly(ContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * 
	 * @see ContentDao#assemblyCount(ContentBean)
	 */
	public QueryCondition assemblyCount(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * @see ContentDao#count(ContentBean)
	 */
	public int count(ContentBean bean) {
		return ((Long) iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * 
	 * @see ContentDao#assemblyNameQuery(ContentBean)
	 */
	public QueryCondition assermblyNameQuery(ContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * 
	 * @see ContentDao#assemblyNameQueryCount(ContentBean)
	 */
	public QueryCondition assemblyNameQueryCount(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * 
	 * @see ContentDao#findByPage(Page, ContentBean)
	 */
	public List<Content> findByPage(Page<Content> page, ContentBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset()); 
		return findByQc(qc);
	}

	// 业务查询主要逻辑代码
	private void transNameQuery(ContentBean bean, QueryCondition qc) {
		qc.append("And obj.contentTitle is not null ");
		if (bean != null) {
			if(StringUtils.isNotBlank(bean.getContentId())){
				qc.add(" and obj.contentId  = ? ", bean.getContentId());
			}
			if (StringUtils.isNotBlank(bean.getKeyword())) {
				qc.add("And (obj.contentTitle like ? ", "%" + bean.getKeyword() + "%");
				qc.add("Or obj.keywords like ? ", "%" + bean.getKeyword() + "%");
				qc.add("Or obj.contentDescription like ? ", "%" + bean.getKeyword() + "%");
				qc.add("Or obj.contentAuthor like ? )", "%" + bean.getKeyword() + "%");
			}
			
			if (StringUtils.isNotBlank(bean.getContentTitle())) {
				qc.add("And obj.contentTitle like ? ", bean.getContentTitle() + "%");
			}
			if (StringUtils.isNotBlank(bean.getKeywords())) {
				qc.add("And obj.keywords like ? ", bean.getKeywords() + "%");
			}
			if (bean.getContentContext() != null) {
				qc.add("And obj.contentContext like ? ", bean.getContentContext() + "%");
			}
			if (StringUtils.isNotBlank(bean.getContentDescription())) {
				qc.add("And obj.contentDescription like ? ", bean.getContentDescription() + "%");
			}
			if (StringUtils.isNotBlank(bean.getContentAuthor())) {
				qc.add("And obj.contentAuthor like ? ", bean.getContentAuthor() + "%");
			}
			if (bean.getCreateTime() != null) {
				qc.add("And obj.createTime = ? ", bean.getCreateTime());
			}
			if (StringUtils.isNotBlank(bean.getCreateLoginName())) {
				qc.add("And obj.createLoginName like ? ", bean.getCreateLoginName() + "%");
			}
			if (StringUtils.isNotBlank(bean.getCreateRealName())) {
				qc.add("And obj.createRealName like ? ", bean.getCreateRealName() + "%");
			}
			if (StringUtils.isNotBlank(bean.getUpdateLoginName())) {
				qc.add("And obj.updateLoginName like ? ", bean.getUpdateLoginName() + "%");
			}
			if (StringUtils.isNotBlank(bean.getUpdateRealName())) {
				qc.add("And obj.updateRealName like ? ", bean.getUpdateRealName() + "%");
			}
			if (bean.getUpdateTime() != null) {
				qc.add("And obj.updateTime = ? ", bean.getUpdateTime());
			}
			if (StringUtils.isNotBlank(bean.getAuditLoginName())) {
				qc.add("And obj.auditLoginName like ? ", bean.getAuditLoginName() + "%");
			}
			if (StringUtils.isNotBlank(bean.getAuditRealName())) {
				qc.add("And obj.auditRealName like ? ", bean.getAuditRealName() + "%");
			}
			if (bean.getAuditTime() != null) {
				qc.add("And obj.auditTime = ? ", bean.getAuditTime());
			}
			if (StringUtils.isNotBlank(bean.getPublishLoginName())) {
				qc.add("And obj.publishLoginName like ? ", bean.getPublishLoginName() + "%");
			}
			if (StringUtils.isNotBlank(bean.getPublishRealName())) {
				qc.add("And obj.publishRealName like ? ", bean.getPublishRealName() + "%");
			}
			if (bean.getPublishTime() != null) {
				qc.add("And obj.publishTime = ? ", bean.getPublishTime());
			}
			if (StringUtils.isNotBlank(bean.getContentStatus())) {
				qc.add("And obj.contentStatus = ? ", bean.getContentStatus());
			}
			if (StringUtils.isNotBlank(bean.getContentTpl())) {
				qc.add("And obj.contentTpl like ? ", bean.getContentTpl() + "%");
			}
			if (bean.getIsTop() != null) {
				qc.add("And obj.isTop = ? ", bean.getIsTop());
			}
			if (StringUtils.isNotBlank(bean.getContentSource())) {
				qc.add("And obj.contentSource like ? ", bean.getContentSource() + "%");
			}
			if (bean.getCategory() != null && StringUtils.isNotBlank(bean.getCategory().getCategoryId())) {
				qc.add("And obj.category.categoryId like ? ", bean.getCategory().getCategoryId());
			}
			if (bean.getTopic() != null && StringUtils.isNotBlank(bean.getTopic().getTopicId())) {
				qc.add("And obj.topic.topicId = ? ", bean.getTopic().getTopicId());
			}
			if (bean.getMaxPublishTime() != null) {
				qc.add("And obj.publishTime < ? ", bean.getMaxPublishTime());
			}
			if (StringUtils.isNotBlank(bean.getSiteId())) {
				qc.add("And obj.siteId = ? ", bean.getSiteId());
				qc.append("And obj.category.categoryId is not null");
			} else if (bean.getCategory() != null && bean.getCategory().getSite() != null
					&& StringUtils.isNotBlank(bean.getCategory().getSite()
							.getSiteId())) {
				qc.add("And obj.category.site.siteId = ? ", bean.getCategory()
						.getSite().getSiteId());
			}	
			if (StringUtils.isNotBlank(bean.getDocType())){
				qc.add(" And obj.class = ?", bean.getDocType().trim());
			}
			if(StringUtils.isNotBlank(bean.getTop())){
				qc.add("And subString(obj.showType,1,1) = ? ", "1");
				bean.setTop("1");
			}
			if(StringUtils.isNotBlank(bean.getOrig()) && StringUtils.isBlank(bean.getTrans())){
				qc.add("And subString(obj.showType,2,1) = ? ","1");
				bean.setOrig("1");
			}
			if(StringUtils.isBlank(bean.getOrig()) && StringUtils.isNotBlank(bean.getTrans())){
				qc.add("And subString(obj.showType,2,1) = ? ", "0");
				bean.setTrans("1");
			}
			if(StringUtils.isNotBlank(bean.getNewest())){
				qc.add("And subString(obj.showType,3,1) = ? ", "1");
				bean.setNewest("1");
			}
			if(StringUtils.isNotBlank(bean.getRecom())){
				qc.add("And subString(obj.showType,4,1) = ? ", "1");
				bean.setRecom("1");
			}
			if(StringUtils.isNotBlank(bean.getPick())){
				qc.add("And subString(obj.showType,5,1) = ? ", "1");
				bean.setPick("1");
			}
			if(StringUtils.isNotBlank(bean.getHotest())){
				qc.add("And subString(obj.showType,6,1) = ? ", "1");
				bean.setHotest("1");
			}
			if(bean.getStartTime()!=null && bean.getEndTime() != null){
				qc.add("And obj.updateTime >= ? ", bean.getStartTime());
				qc.add("And obj.updateTime <= ? ", bean.getEndTime());
			}else if(bean.getStartTime() == null && bean.getEndTime() != null){
				qc.add("And obj.updateTime <= ? ", bean.getEndTime());
			}else if(bean.getStartTime() != null && bean.getEndTime() == null){
				qc.add("And obj.updateTime >= ? ", bean.getStartTime());
			}
			if(bean.getCatIds() != null){
				if(!bean.getCatIds().isEmpty()){
					qc.append(" And ( ");
					int i=bean.getCatIds().size();
					for(String catId: bean.getCatIds()){
						i--;
						qc.add(" obj.category.categoryId = ? ", catId);
						if(i!=0) qc.append(" or ");
					}
					qc.append(" ) ");
				}
				if(StringUtils.isBlank(bean.getContentStatus())){
					qc.append(" And (obj.contentStatus = 0 or obj.contentStatus = 2)");
				}
			}
			if(StringUtils.isNotBlank(bean.getAdminId()) && StringUtils.isBlank(bean.getContentStatus())){
				qc.append(" And (obj.contentStatus = 0 or obj.contentStatus = 2) ");
			}
			if(StringUtils.isNotBlank(bean.getTflag()) && "shenhe".equals(bean.getTflag())){
				qc.append(" And (obj.contentStatus = 0 or obj.contentStatus = 2)");
			}
			if(bean.getCatIdset() != null){
				/* bean.getCatIdset（） == null  说明当前是超级管理员在操作 无需通过权限栏目权限进行限制 
				 * bean.getCatIdset（）  isEmpty 说明 非超级管理员而且没有 相关的栏目内容权限  将不显示 查询结果 
				 *  bean.getCatIdset（） 不为空，按条件进行查询
				 * */
				if(bean.getCatIdset().isEmpty()){
					qc.append(" And 1!=1 ");
				}else{
					qc.append(" And ( ");
					int i=bean.getCatIdset().size();
					for(String catId: bean.getCatIdset()){
						i--;
						qc.add(" obj.category.categoryId = ? ", catId);
						if(i!=0) qc.append(" or ");
					}
					qc.append(" ) ");
				}				
			}			
			if(bean != null && StringUtils.isNotBlank(bean.getOrderBy())){
				if("up".equals(bean.getOrderFalg())){
					qc.appenOrderBy("  nlssort(obj."+bean.getOrderBy()+",'NLS_SORT=SCHINESE_PINYIN_M') asc");					
				}else if("down".equals(bean.getOrderFalg())){
					qc.appenOrderBy("  nlssort(obj."+bean.getOrderBy()+",'NLS_SORT=SCHINESE_PINYIN_M') desc");	
				}
			}
			qc.appenOrderBy(" obj.updateTime desc");
			qc.appenOrderBy(" obj.createTime desc,obj.publishTime desc");
			qc.appenOrderBy(" obj.contentId desc");
		}
	}

	/**
	 * 获取非属性对象的条件
	 * 
	 * @param page
	 * @param bean
	 * @return
	 */
	public List<Content> findByPageNotIn(Page<Content> page, ContentBean bean) {
		QueryCondition qc = assermblyNotInQuery(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}

	/**
	 * 根据BEAN组装非属性的查询条件
	 * 
	 * @param bean
	 * @return
	 */
	public QueryCondition assermblyNotInQuery(ContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNotInQuery(bean, qc);
		return qc;
	}

	public QueryCondition assermblyNotInQueryCount(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNotInQuery(bean, qc);
		return qc;
	}

	public int countNotIn(ContentBean bean) {
		return ((Long) iterator(assermblyNotInQueryCount(bean)).next()).intValue();
	}

	public int countAllByPublish(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transPublishQuery(bean, qc);
		return ((Long) iterator(qc).next()).intValue();
	}

	public List<Content> findPublish(ContentBean contentBean, int currentContentIndex, int max) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transPublishQuery(contentBean, qc);
		qc.setMaxResults(max);
		qc.setOffset(currentContentIndex);
		qc.append("Order by obj.contentId desc");
		return findByQc(qc);
	}

	public List<Content> findPublish(ContentBean contentBean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transPublishQuery(contentBean, qc);
		qc.append("Order by obj.contentId desc");
		return findByQc(qc);
	}
	
	public List<Content> findPublish(ContentBean contentBean,int pageSize) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transPublishQuery(contentBean, qc);
		qc.setMaxResults(pageSize);
		qc.append("Order by obj.publishTime desc");
		return findByQc(qc);
	}

	private void transPublishQuery(ContentBean bean, QueryCondition qc) {
		if (bean != null) {
			if(bean.getCategory() != null){
				if (StringUtils.isNotBlank(bean.getCategory().getCategoryId())) {
					qc.add("And obj.category.categoryId = ? ", bean.getCategory().getCategoryId());
				}else{
					if( bean.getCategory().getSite() != null && StringUtils.isNotBlank(bean.getCategory().getSite().getSiteId())) {
						qc.add("And obj.category.site.siteId = ? ", bean.getCategory().getSite().getSiteId());
					}
				}
				if(bean.getCategory().getIsAudit()!=null){
						if(bean.getCategory().getIsAudit().booleanValue()){
							qc.add("And (obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
							qc.add(" or obj.contentStatus = ?) ", DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
						}else{
							qc.add("And (obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
							qc.add(" or obj.contentStatus = ?) ", DictionaryStaticValues.CONTENT_STATUS_NEW);
						}
				}
			}
			if(bean.getTopic() != null){
				 if(StringUtils.isNotBlank(bean.getTopic().getTopicId())) {				
					 qc.add("And obj.topic.topicId = ? ", bean.getTopic().getTopicId());
				 }
			}
			if(bean.getPublishType()!=null){
				if(DictionaryStaticValues.CONTENT_PUBLISH_TYPE_DEFAULT.equals((bean.getPublishType().toString()))){
					qc.add("And obj.publishType = ? ", bean.getPublishType());
				}
			}
			if (bean.getMaxPublishTime() != null) {
				qc.add("And obj.publishTime < ? ", bean.getMaxPublishTime());
			}
			if(StringUtils.isNotBlank(bean.getContentStatus())){
				qc.add("And obj.contentStatus = ? ", bean.getContentStatus());
			}
			qc.add("And ( obj.publishTime < ? ", new Date());
			qc.append(" or obj.publishTime is null )");
			qc.append(" And obj.class !=LinkContent");
			
		}
	}

	// NOTIN业务查询主要逻辑代码
	private void transNotInQuery(ContentBean bean, QueryCondition qc) {
		String keys = bean.getKeywords().replace("，", ",");
		String keyws = keys.replace(" ", ",");
		String[] keyword = keyws.split(",");
		if (bean != null) {
			if (bean.getCategory() != null && StringUtils.isNotBlank(bean.getCategory().getCategoryId())) {
				qc.add("And (obj.category.categoryId <> ? Or obj.category.categoryId is null)", bean.getCategory().getCategoryId());
			}
			if (bean.getTopic() != null && StringUtils.isNotBlank(bean.getTopic().getTopicId())) {
				qc.add("And (obj.topic.topicId <> ? Or obj.topic.topicId is null)", bean.getTopic().getTopicId());
			}
			if (keyword != null) {
				qc.append("And (");
				for (int i = 0; i < keyword.length; i++) {
					qc.add("obj.keywords like ? ", "%" + keyword[i] + "%");
					if (i < keyword.length - 1) {
						qc.append(" or");
					}
				}
				qc.append(")");
			}
		}
		if (StringUtils.isNotBlank(bean.getContentStatus())) {
			qc.add(" And obj.contentStatus = ? ", bean.getContentStatus());
		}
	}

	public List<Content> ftlFindAll(String[] docIds, String flag, String[] catIds, String[] contentTypes, String begintime, String endTime, String[] keywords, String condition, String orderBy,
			long[] limit, boolean pagination, long pageSize, long currentPage) {

		QueryCondition qc = new QueryCondition(MAIN_HQL);

		createQuery(qc, docIds, contentTypes, flag, catIds, begintime, endTime, keywords, condition, orderBy, limit, pagination, pageSize, currentPage);
		List<Content> list = findByQc(qc);
		this.initializeContents(list);
		return list;
	}
	
	public List<Content> darkblueFindAll(String[] siteId, String[] docIds, String flag, String[] catIds, String[] topicIds,
			String[] contentTypes, String begintime, String endTime,
			String[] keywords, String condition, String orderBy, long[] limit,
			boolean pagination, long pageSize, long crrunetPage,
			boolean showLike, boolean rondom, long maxPageCount, String articleType){
		
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		
		darkblueCreateQuery(qc, siteId, docIds, contentTypes, flag, catIds, topicIds, begintime, endTime, keywords, condition, orderBy, limit, pagination, pageSize, crrunetPage, articleType);
		List<Content> list = findByQc(qc);
		this.initializeContents(list);
		return list;
	}

	private void initializeContents(Collection<Content> contents) {
		for (Content c : contents) {
			this.initialize(c.getContentAttachs());
		}
	}

	private void createQuery(QueryCondition qc, String[] docIds, String[] contentTypes, String flag, String[] catIds, String begintime, String endTime, String[] keywords, String condition,
			String orderBy, long[] limit, boolean pagination, long pageSize, long currentPage) {
		if (!ArrayUtils.isEmpty(catIds)) {
			qc.append("And obj.category.categoryId  in " + HqlUtil.arraysToString(catIds));
		}
		if (!ArrayUtils.isEmpty(keywords)) {
			qc.append("and ( ");
			for (int i = 0; i < keywords.length; i++) {
				if (i == 0) {
					qc.add(" obj.keywords  like ? ", "%" + keywords[i] + "%");
				} else {
					qc.add(" or obj.keywords  like ? ", "%" + keywords[i] + "%");
				}
			}
			qc.append(" ) ");
		}

		qcAddCommonInfo(qc, docIds, contentTypes, flag, begintime, endTime, condition, orderBy, limit, pagination, pageSize, currentPage);
	}
	
	private void darkblueCreateQuery(QueryCondition qc, String[] siteId, String[] docIds, String[] contentTypes, String flag, String[] catIds, String[] topicIds, String begintime, String endTime, String[] keywords, String condition,
			String orderBy, long[] limit, boolean pagination, long pageSize, long currentPage, String articleType) {
		if (!ArrayUtils.isEmpty(siteId)) {
			qc.append(" And obj.category.site in " + HqlUtil.arraysToString(siteId));
		}
		if (!ArrayUtils.isEmpty(catIds)) {
			qc.append("And obj.category.categoryId  in " + HqlUtil.arraysToString(catIds));
		}
		if(!ArrayUtils.isEmpty(topicIds)) {
			qc.append(" And obj.topic.topicId in " + HqlUtil.arraysToString(topicIds));
		}
		if (!ArrayUtils.isEmpty(keywords)) {
			qc.append("and ( ");
			for (int i = 0; i < keywords.length; i++) {
				if (i == 0) {
					qc.add(" obj.keywords  like ? ", "%" + keywords[i] + "%");
				} else {
					qc.add(" or obj.keywords  like ? ", "%" + keywords[i] + "%");
				}
			}
			qc.append(" ) ");
		}
		
		if("0".equals(articleType)) {
			specialQc(qc, docIds, contentTypes, flag, begintime, endTime, condition, orderBy, limit, pagination, pageSize, currentPage);
		}else {
			qcAddCommonInfo(qc, docIds, contentTypes, flag, begintime, endTime, condition, orderBy, limit, pagination, pageSize, currentPage);
		}
	}

	/**
	 * 根据栏目id查询
	 * 
	 * @param catId
	 * @return
	 */
	public List<Content> findByCatId(String catId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.category.categoryId = ? ", catId);
		return findByQc(qc);
	}

	public List<Content> findByTopicId(String topicId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add("And obj.topic.topicId = ? ", topicId);
		return findByQc(qc);
	}

	public List<Content> findAll(ContentBean contentBean) {
		QueryCondition qc = assembly(contentBean);
		return findByQc(qc);
	}

	public List<Content> ftlFindAllTopic(String[] docIds, String flag, String topicId, String[] keywords, String[] excludeIds, String[] contentTypes, String begintime, String endTime,
			String condition, String orderBy, long[] limit, boolean pagination, long pageSize, long crrunetPage, boolean showLike, boolean rondom, long maxPageCount) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);

		if (StringUtils.isNotBlank(topicId)) {
			if (!ArrayUtils.isEmpty(keywords)) {
				qc.append("and ( ");
				qc.add("  obj.topic.topicId = ? ", topicId);
				for (int i = 0; i < keywords.length; i++) {
					qc.add(" or obj.keywords  like ? ", "%" + keywords[i] + "%");
				}
				qc.append(" ) ");
			} else {
				qc.add(" and obj.topic.topicId = ? ", topicId);
			}
			if (!ArrayUtils.isEmpty(excludeIds)) {
				qc.append(" and obj.contentId not in " + HqlUtil.arraysToString(excludeIds));
			}

		}
		qcAddCommonInfo(qc, docIds, contentTypes, flag, begintime, endTime, condition, orderBy, limit, pagination, pageSize, crrunetPage);

		List<Content> list = findByQc(qc);
		this.initializeContents(list);
		return list;
	}

	private void qcAddCommonInfo(QueryCondition qc, String[] docIds, String[] contentTypes, String flag, String begintime, String endTime, String condition, String orderBy, long[] limit,
			boolean pagination, long pageSize, Long currentPage) {
		qc.add("And ( obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		qc.add(" or obj.contentStatus = ? ) ", DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
		if (!ArrayUtils.isEmpty(docIds)) {
			qc.append("And obj.contentId  in  " + HqlUtil.arraysToString(docIds));
			LOG.info(HqlUtil.arraysToString(docIds));
		}
		if (contentTypes != null && contentTypes.length > 0) {
			qc.append(" and (");
			for (int j = 0; j < contentTypes.length; j++) {
				qc.append(" obj.class=" + contentTypes[j]);

				if (j != contentTypes.length - 1)
					qc.appendQueryString(" or ");
			}
			qc.append(" ) ");
		}
		if (StringUtils.isNotBlank(flag)) {
			List<Integer> list = HqlUtil.splitFlagIndex(flag);
			if (list != null && !list.isEmpty()) {
				qc.append(" And (");

				for (int i = 0; i < list.size(); i++) {
					qc.add(" subString (obj.showType,?,1) ='1' ", list.get(i));
					if (i != list.size() - 1)
						qc.append(" or ");
				}
				qc.append(" ) ");
			}
		}

		if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isNotBlank(begintime) && StringUtils.isBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
		}

		if (StringUtils.isNotBlank(condition)) {
			qc.append("and " + condition);
		}
		if (StringUtils.isNotBlank(orderBy)) {
			qc.appenOrderBy(orderBy);
		}
		qc.append(" And obj.publishTime is not null");
		qc.appenOrderBy(" obj.isTop desc,obj.ord asc ");
		qc.appenOrderBy(" obj.publishTime desc ");

		if (!ArrayUtils.isEmpty(limit) && limit.length < 3) {
			pagination = false;
			if (limit.length == 1) {

				qc.setMaxResults((int) limit[0]);
				qc.setOffset(0);
			}
			if (limit.length == 2) {
				qc.setMaxResults((int) limit[1]);
				qc.setOffset((int) limit[0]);
			}
		}

		if (pagination) {
			if (pageSize < 1L)
				pageSize = 10L;
			if (currentPage == null)
				currentPage = 1L;
			Page<Content> page = new Page<Content>();
			page.setPageSize((int) pageSize);
			page.setPageNo(currentPage.intValue());
			qc.setMaxResults(page.getPageSize());
			qc.setOffset(page.getOffset());
		}
		LOG.info("\n" + qc.getQueryString());
	}
	
	private void specialQc(QueryCondition qc, String[] docIds, String[] contentTypes, String flag, String begintime, String endTime, String condition, String orderBy, long[] limit,
			boolean pagination, long pageSize, Long currentPage) {
		//qc.append("And obj.contentStatus  in ('" + DictionaryStaticValues.CONTENT_STATUS_SUCCESS + "','" + DictionaryStaticValues.CONTENT_STATUS_PUBLISH + "')");
		qc.add("And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		if (!ArrayUtils.isEmpty(docIds)) {
			qc.append("And obj.contentId  in  " + HqlUtil.arraysToString(docIds));
			LOG.info(HqlUtil.arraysToString(docIds));
		}
		if (contentTypes.length > 0) {
			qc.append(" and (");
			for (int j = 0; j < contentTypes.length; j++) {
				qc.append(" obj.class=" + contentTypes[j]);

				if (j != contentTypes.length - 1)
					qc.appendQueryString(" or ");
			}
			qc.append(" ) ");
		}
		if (StringUtils.isNotBlank(flag)) {
			List<Integer> list = HqlUtil.splitFlagIndex(flag);
			if (list != null && !list.isEmpty()) {
				qc.append(" And  ( ( ");
				qc.append(" ( ");
				for (int i = 0; i < list.size(); i++) {
					qc.add(" subString (obj.showType,?,1) ='1' ", list.get(i));
					if (i != list.size() - 1)
						qc.append(" or ");
				}
				qc.append("  ) ");
				qc.append(" and obj.class='PictureContent' ) or obj.class='Content' ) ");
			}
		}
		
		
		if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isNotBlank(begintime) && StringUtils.isBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
		}

		if (StringUtils.isNotBlank(condition)) {
			qc.append("and " + condition);
		}
		if (StringUtils.isNotBlank(orderBy)) {
			qc.appenOrderBy(orderBy);
		}
		qc.append(" And obj.publishTime is not null");
		qc.appenOrderBy(" obj.isTop desc");
		qc.appenOrderBy(" obj.publishTime desc ");

		if (!ArrayUtils.isEmpty(limit) && limit.length < 3) {
			if (limit.length == 1) {

				qc.setMaxResults((int) limit[0]);
				qc.setOffset(0);
			}
			if (limit.length == 2) {
				qc.setMaxResults((int) limit[1]);
				qc.setOffset((int) limit[0]);
			}
		}

		if (pagination) {
			if (pageSize < 1L)
				pageSize = 10L;
			if (currentPage == null)
				currentPage = 1L;
			Page<Content> page = new Page<Content>();
			page.setPageSize((int) pageSize);
			page.setPageNo(currentPage.intValue());
			qc.setMaxResults(page.getPageSize());
			qc.setOffset(page.getOffset());
		}
		LOG.info("\n" + qc.getQueryString());
	}

	public int findTotalNum(QueryCondition qc) {
		if (qc == null)
			return 0;
		return ((Long) iterator(qc).next()).intValue();
	}

	/**
	 * 检查站点名称唯一性
	 */
	public Boolean checkNameUnique(String contentTitle) {
		boolean unique = false;
		List<Content> conList = null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if (StringUtils.isNotBlank(contentTitle)) {
			qc.add(" And obj.contentTitle =? ", contentTitle);
			conList = findByQc(qc);
		}

		if ((conList != null) && (!conList.isEmpty())) {
			unique = true;
		}
		return unique;
	}

	/**
	 * 根据栏目ID获取指定数量的文章
	 * 
	 * @param catId
	 * @return
	 */
	public List<Content> findContentByCatId(String catId, Long num) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if (StringUtils.isNotBlank(catId)) {
			qc.add(" And obj.category.categoryId = ? ", catId);
		}
		if (num != 0) {
			// qc.add(" And rownum >= ? ", num);
			qc.append(" And rownum <= 5");
		}
		return findByQc(qc);
	}
	public int searchCount(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		getSearchQc(qc, bean);
		return ((Long) iterator(qc).next()).intValue();
	}
	
	public int searchPlanCount(ContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		getSearchPlanQc(qc, bean);
		return ((Long) iterator(qc).next()).intValue();
	}
	
	public List<Content> searchPage(Page<Content> page, ContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		getSearchQc(qc, bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append("Order by obj.contentHits desc,obj.publishTime desc");
		return findByQc(qc);
	}
	public List<Content> searchPlanPage(Page<Content> page, ContentBean bean){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		getSearchPlanQc(qc, bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append("Order by obj.contentHits desc,obj.publishTime desc");
		return findByQc(qc);
	}
	private void getSearchPlanQc(QueryCondition qc,ContentBean bean){ 
		String catid=bean.getCategory().getCategoryId();
		if(catid.contains(",")){
			String[] catIds = bean.getCategory().getCategoryId().trim().split(",");
			qc.add("And obj.contentStatus= ? " , DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
			qc.add("And (obj.category.id= ? ", catIds[0]);
			for(int i=1;i<catIds.length;i++){
				qc.add("or obj.category.id =? ", catIds[i]);
			}
			qc.append(")");
		}else{
			qc.add("And obj.contentStatus= ? " , DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
			qc.add("And obj.category.id= ? ", catid);
		}
		if (StringUtils.isNotBlank(bean.getKeyword())) {			
			String[] keys = bean.getKeyword().split(" ");
			qc.add(" And (obj.contentTitle like ? ", "%" + keys[0] + "%");
			qc.add(" or obj.contentContext like ? ", "%" + keys[0] + "%");
			//qc.add(" or obj.contentAuthor like ? ", "%" + keys[0] + "%");
			for (int i=1;i<keys.length;i++) {
				qc.add(" or obj.contentTitle like ? ", "%" + keys[i] + "%");
				qc.add(" or obj.contentContext like ? ", "%" + keys[i] + "%");
				//qc.add(" or obj.contentAuthor like ? ", "%" + keys[i] + "%");
			}
			qc.append(")");
			
		}
		
	}
	private void getSearchQc(QueryCondition qc, ContentBean bean) {
		
		if(bean.getCategoryId().length==1){
			qc.add(" And obj.category.id = ? ", bean.getCategoryId()[0]);
		}
		else if(bean.getCategoryId().length>1){
			qc.append(" And (");
			qc.add(" obj.category.id = ? ", bean.getCategoryId()[0]);
			for(int i=1;i<bean.getCategoryId().length;i++){
				qc.add(" Or obj.category.id = ? ", bean.getCategoryId()[i]);
			}
			qc.append(")");
		}	
		
		if (StringUtils.isNotBlank(bean.getKeyword())) {			
			String[] keys = bean.getKeyword().split(" ");
			for (String key:keys) {
				qc.add(" And (obj.contentTitle like ? ", "%" + key + "%");
				qc.add(" or obj.contentContext like ? ", "%" + key + "%");
				qc.add(" or obj.contentDescription like ? )", "%" + key + "%");				
			}
		}
		qc.add("And obj.contentStatus= ? " , DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
	}
	/**
	 * 根据栏目id查询文章-多个栏目id
	 * @param catList
	 * @return
	 */
	public List<Content>findByCats(List<String>catList){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(catList != null){
			qc.append(" And ( ");
			int i=catList.size();
			for(String catId: catList){
				i--;
				qc.add(" obj.category.categoryId = ? ", catId);
				if(i!=0) qc.append(" or ");
			}
			qc.append(" ) ");
		}
		qc.append(" And obj.contentStatus <> 0");
		return findByQc(qc);
	}

	public List<Content> findByIds(String[] ids) {
		if(ArrayUtils.isEmpty(ids)) return null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		int i = 0;
		qc.append(" and ( ");
		for(String id :ids ){
			qc.add(" obj.contentId = ?  ", id);
			i++;
			if(i!=ids.length) qc.append(" or ");
		}
		qc.append(" ) ");
		return findByQc(qc);
	}
	
	
	public int ftlFindAllCount(String[] docIds, String flag, String[] catIds, String[] contentTypes, String begintime, String endTime, String[] keywords, String condition) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		if (!ArrayUtils.isEmpty(catIds)) {
			qc.append("And obj.category.categoryId  in " + HqlUtil.arraysToString(catIds));
		}
		if (!ArrayUtils.isEmpty(keywords)) {
			qc.append("and ( ");
			for (int i = 0; i < keywords.length; i++) {
				if (i == 0) {
					qc.add(" obj.keywords  like ? ", "%" + keywords[i] + "%");
				} else {
					qc.add(" or obj.keywords  like ? ", "%" + keywords[i] + "%");
				}
			}
			qc.append(" ) ");
		}
		qcCountCommonInfo(qc, docIds, contentTypes, flag, begintime, endTime, condition);				
		return ((Long) iterator(qc).next()).intValue();
	}

	public int ftlFindAllTopicCount(String[] docIds, String flag, String topicId, String[] keywords, 
			String[] excludeIds, String[] contentTypes, String begintime, String endTime,String condition, boolean showLike) {
		
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		if (StringUtils.isNotBlank(topicId)) {
			if (!ArrayUtils.isEmpty(keywords)) {
				qc.append("and ( ");
				qc.add("  obj.topic.topicId = ? ", topicId);
				for (int i = 0; i < keywords.length; i++) {
					qc.add(" or obj.keywords  like ? ", "%" + keywords[i] + "%");
				}
				qc.append(" ) ");
			} else {
				qc.add(" and obj.topic.topicId = ? ", topicId);
			}
			if (!ArrayUtils.isEmpty(excludeIds)) {
				qc.append(" and obj.contentId not in " + HqlUtil.arraysToString(excludeIds));
			}

		}
		qcCountCommonInfo(qc, docIds, contentTypes, flag, begintime, endTime, condition);				
		return ((Long) iterator(qc).next()).intValue();
	}

	private void qcCountCommonInfo(QueryCondition qc, String[] docIds, String[] contentTypes, String flag, String begintime, String endTime, String condition) {
		//qc.append("And obj.contentStatus  in ('" + DictionaryStaticValues.CONTENT_STATUS_SUCCESS + "','" + DictionaryStaticValues.CONTENT_STATUS_PUBLISH + "')");
		qc.add("And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		if (!ArrayUtils.isEmpty(docIds)) {
			qc.append("And obj.contentId  in  " + HqlUtil.arraysToString(docIds));
			LOG.info(HqlUtil.arraysToString(docIds));
		}
		if (contentTypes.length > 0) {
			qc.append(" and (");
			for (int j = 0; j < contentTypes.length; j++) {
				qc.append(" obj.class=" + contentTypes[j]);

				if (j != contentTypes.length - 1)
					qc.appendQueryString(" or ");
			}
			qc.append(" ) ");
		}
		if (StringUtils.isNotBlank(flag)) {
			List<Integer> list = HqlUtil.splitFlagIndex(flag);
			if (list != null && !list.isEmpty()) {
				qc.append(" And (");

				for (int i = 0; i < list.size(); i++) {
					qc.add(" subString (obj.showType,?,1) ='1' ", list.get(i));
					if (i != list.size() - 1)
						qc.append(" or ");
				}
				qc.append(" ) ");
			}
		}

		if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isBlank(begintime) && StringUtils.isNotBlank(endTime)) {
			qc.add("And obj.publishTime <= ? ", DateUtil.getDayEnd(DateUtil.formatStringToDate(endTime, "yyyy-MM-dd")));
		} else if (StringUtils.isNotBlank(begintime) && StringUtils.isBlank(endTime)) {
			qc.add("And obj.publishTime >= ? ", DateUtil.getDayStart(DateUtil.formatStringToDate(begintime, "yyyy-MM-dd")));
		}

		if (StringUtils.isNotBlank(condition)) {
			qc.append("and " + condition);
		}
		qc.append(" And obj.publishTime is not null");
		qc.appenOrderBy(" obj.isTop desc,obj.ord asc");
		qc.appenOrderBy(" obj.publishTime desc ");
		LOG.info("\n" + qc.getQueryString());
	}
	
	/**
	 * 根据文章id获得栏目名称
	 * @param contentId
	 * @return
	 */
    public String getCatName(String contentId){
    	QueryCondition qc = new QueryCondition(MAIN_HQL);
    	List<Content> list = null;
    	qc.add(" And obj.contentId = ? ", contentId);
    	list = findByQc(qc);
    	String catName = list.get(0).getCategory().getCatName();
    	return catName;
    }

	
	public List<Content> getSameZskContent(String contentId, int count) {
		if(count<=0){
			return new ArrayList<Content>();
		}
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		Content c = this.findById(contentId);
		this.initialize(c);
		String keyword = c.getKeywords();
		qc.add(" and obj.contentId!= ? ", contentId);
		qc.add(" and obj.category.id= ? ", c.getCategory().getCategoryId());//同栏目
		if(StringUtils.isNotBlank(keyword)){
			String[] keywords = keyword.split("\\s{1,}|,");
			qc.add(" and(obj.keywords like ? ", "%"+keywords[0]+"%");
			for(int i=1;i<keywords.length;i++){
				qc.add(" or obj.keywords like ? ", "%"+keywords[i]+"%");				
			}
			qc.append(")");
		}
		qc.setOffset(0);
		qc.setMaxResults(count);
		return  findByQc(qc);
		
	}
	
	public List<Content> getSiteSameContent(Content c ,int count,List<Category> catList){
		if(count<=0||catList==null){
			return new ArrayList<Content>();
		}
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		
		String keyword = c.getKeywords();
		qc.add(" And obj.contentId!= ? ", c.getContentId());
		if(!catList.isEmpty()){
			qc.append(" And (");
			for(int i=0;i<catList.size();i++){
				qc.add("obj.category.id= ? ", catList.get(i).getCategoryId());
				if(i!=catList.size()-1){
					qc.append(" or ");
				}
			}	
			qc.append(")");
		}
		
		if(StringUtils.isNotBlank(keyword)){
			String[] keywords = keyword.split("\\s{1,}|,");
			qc.add(" and(obj.keywords like ? ", "%"+keywords[0]+"%");
			for(int i=1;i<keywords.length;i++){
				qc.add(" or obj.keywords like ? ", "%"+keywords[i]+"%");				
			}
			qc.append(")");
		}
		else{
			return new ArrayList<Content>();
		}
		qc.setOffset(0);
		qc.setMaxResults(count);
		return  findByQc(qc);
	}
	
	public List<Content> ftlFindTopPosts(int num){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.append(" And obj.contentHits is not null");
		qc.appenOrderBy(" obj.contentHits desc");
		qc.setOffset(0);
		qc.setMaxResults(num);
		List<Content> list = findByQc(qc);
		return list;
	}

	public List<Content> ListPublishContent(int offset, int maxSize, String[] catIds, String[] noCatIds) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		if(catIds!=null&&catIds.length>0){
			qc.append(" And ( ");
			int i = catIds.length;
			for(String catId: catIds){
				i--;
				qc.add(" obj.category.categoryId = ? ", catId);
				if(i!=0) qc.append(" or ");
			}
			qc.append(" ) ");
		}
		
		if(noCatIds!=null&&noCatIds.length>0){
			for(String catId: noCatIds){
				qc.add(" And obj.category.categoryId != ? ", catId);
			}
		}	
		qc.setOffset(offset);
		qc.setMaxResults(maxSize);
		return findByQc(qc);
	}

	public int countPublishContent(String[] catIds, String[] noCatIds) {
		QueryCondition qc = new QueryCondition(COUNT_HQL+MAIN_HQL);
		qc.add(" And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		if(catIds!=null&&catIds.length>0){
			qc.append(" And ( ");
			int i = catIds.length;
			for(String catId: catIds){
				i--;
				qc.add(" obj.category.categoryId = ? ", catId);
				if(i!=0) qc.append(" or ");
			}
			qc.append(" ) ");
		}
		
		if(noCatIds!=null&&noCatIds.length>0){
			for(String catId: noCatIds){
				qc.add(" And obj.category.categoryId != ? ", catId);
			}
		}	
		return ((Long) iterator(qc).next()).intValue();
	}

	public Content findDocBycatIds(Set<String> set, String contentTypes) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.append(" And  obj.class =  " + contentTypes);
		/*qc.add("And (obj.contentStatus  = ? ",DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
		qc.add(" or obj.contentStatus  = ? )",DictionaryStaticValues.CONTENT_STATUS_PUBLISH);*/
		qc.add("And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		qc.append(" And ( ");
		int i = set.size(); 
		for(String s : set){
			i--;
			qc.add("  obj.category.categoryId = ? ", s);
			if(i!=0 )  qc.append("  or  ");
		}		
		qc.append(" ) "); 
		qc.append(" And obj.publishTime is not null");	 
		qc.appenOrderBy(" obj.publishTime desc ");	
		qc.setMaxResults(1);
		List<Content> list = findByQc(qc);
		if(list.isEmpty())  return new Content();		
		return list.get(0);
	}
	
	public List<Content> finddocsByCatIds(Set<String> set, int num) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);	 
		/*qc.add("And (obj.contentStatus  =? ",DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
		qc.add(" or obj.contentStatus  =? )",DictionaryStaticValues.CONTENT_STATUS_PUBLISH);*/
		qc.add("And obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		qc.append(" And ( ");
		int i = set.size(); 
		for(String s : set){
			i--;
			qc.add("  obj.category.categoryId = ? ", s);
			if(i!=0)  qc.append("  or  ");
		}		
		qc.append(" ) "); 
		qc.append(" And obj.publishTime is not null");	
		qc.appenOrderBy(" obj.ord asc,obj.publishTime desc ");	
		qc.setMaxResults(num);	 
		return findByQc(qc);
	}

	public int findByTitleAndCreatedTime(String contentTitle,
			Date createTime,String categoryId) {
		if(StringUtils.isBlank(contentTitle))  return -1;
		if(createTime == null) return -1;
		Criteria c = getSession().createCriteria(Content.class); 
		c.add(Restrictions.eq("contentTitle", contentTitle));
		c.add(Restrictions.eq("createTime", createTime));
		c.add(Restrictions.eq("category.categoryId", categoryId));
		c.setProjection(Projections.rowCount());
		return (Integer)c.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<String> findContentIdsBySiteId(List<String> list,int currentContentIndex, int max) {
		QueryCondition qc = new QueryCondition("Select obj.id "+MAIN_HQL);	 
		qc.add(" obj.category.site.id in(?","-1");//不存在的站点
		for(String siteId : list){
			qc.add(",?", siteId);
		}
		qc.append(")");
		qc.appenOrderBy(" obj.createTime desc");
		qc.appenOrderBy(" obj.id desc ");
		qc.setOffset(currentContentIndex);
		qc.setMaxResults(max);
		return (List<String>)find(qc);
	}

	@SuppressWarnings("unchecked")
	public Iterator<Content> findContentBySiteIds(List<String> siteIds,int currentContentIndex) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.category.site.id in(?","-1");//不存在的站点
		for(String siteId : siteIds){
			qc.add(",?", siteId);
		}
		qc.append(")");
		qc.add(" And (obj.publishTime <? or obj.publishTime is null) ", new Date());
		qc.append(" And obj.class !=LinkContent");
//		qc.add( " or obj.topic.site.id in(?" , "-1");
//		for(String siteId : siteIds){
//			qc.add(",?", siteId);
//		}
//		qc.append("))");
		qc.appenOrderBy(" obj.createTime desc");
		qc.appenOrderBy(" obj.id desc ");
		qc.setOffset(currentContentIndex);
		return (Iterator<Content>) this.iterator(qc);
	}

	@SuppressWarnings("unchecked")
	public Iterator<Content> findMoreContentIdsBySiteIds(List<String> siteIds,int currentContentIndex) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.category.site.id in(?","-1");//不存在的站点
		for(String siteId : siteIds){
			qc.add(",?", siteId);
		}
		qc.append(")");
		qc.add(" And (obj.publishTime <? or obj.publishTime is null) ", new Date());
		qc.append(" And obj.class !=LinkContent");
//		qc.add( " or obj.topic.site.id in(?" , "1");
//		for(String siteId : siteIds){
//			qc.add(",?", siteId);
//		}
//		qc.append("))");
		qc.add(" And obj.contentStatus != ?", DictionaryStaticValues.PUBLISH);
		qc.appenOrderBy(" obj.createTime desc");
		qc.appenOrderBy(" obj.id desc ");
		qc.setOffset(currentContentIndex);
		return (Iterator<Content>) this.iterator(qc);
	} 
}